package com.nanbei.vod.controller;

import com.aliyuncs.exceptions.ClientException;
import com.nanbei.commonUntil.R;
import com.nanbei.servicebase.exceptionHandler.GuliException;
import com.nanbei.vod.service.VodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;

@RestController
@RequestMapping("/vodService/vod")
@CrossOrigin   //解决跨域问题
public class VodController {

    @Autowired
    private VodService vodService;

    /**
     * 阿里云点播视频上传
     * @param file
     * @return
     */
    @PostMapping("/uploadVideoToALi")
    public R uploadVideoToALi(MultipartFile file){
        String videoId = vodService.uploadVideoToALi(file);
        return R.ok().data("videoId",videoId);
    }

    /**
     * 删除单个视频
     * @param videoId 视频id
     * @return
     */
    @DeleteMapping("/delVideoToALi/{videoId}")
    public R delVideoToALi(@PathVariable String videoId){
        try {
            vodService.delVideoToALi(videoId);
            return R.ok();
        } catch (ClientException e) {
            e.printStackTrace();
            throw  new GuliException(20001,"删除视频失败");
        }
    }

    /**
     * 删除多条视频
     * @param videoIds
     * @return
     */
    @DeleteMapping("/delSomeVideos")
    public R delSomeVideos(@RequestParam("videoIds") ArrayList<String> videoIds){
        try {
            vodService.delSomeVideos(videoIds);
            return R.ok();
        } catch (ClientException e) {
            e.printStackTrace();
            throw  new GuliException(20001,"删除视频失败");
        }
    }

    /**
     * 获取视频播放凭证
     * @param vodId
     * @return
     */
    @GetMapping("/getVodPZ/{vodId}")
    public R getVodPZ(@PathVariable String vodId){
        String vodPZ = null;
        try {
            vodPZ = vodService.getVodPZ(vodId);
        } catch (ClientException e) {
            new GuliException(20001,"获取播放凭证失败");
        }
        return R.ok().data("vodPZ",vodPZ);
    }
}
